﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.Party.PartyComponents.GarrisonPartyComponent
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System.Collections.Generic;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.Party.PartyComponents
{
  public class GarrisonPartyComponent : PartyComponent
  {
    [CachedData]
    private TextObject _cachedName;

    internal static void AutoGeneratedStaticCollectObjectsGarrisonPartyComponent(
      object o,
      List<object> collectedObjects)
    {
      ((PartyComponent) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this.Settlement);
    }

    internal static object AutoGeneratedGetMemberValueSettlement(object o)
    {
      return (object) ((GarrisonPartyComponent) o).Settlement;
    }

    public static MobileParty CreateGarrisonParty(
      string stringId,
      Settlement settlement,
      bool isInitialGarrison)
    {
      return MobileParty.CreateParty(stringId, (PartyComponent) new GarrisonPartyComponent(settlement), (PartyComponent.OnPartyComponentCreatedDelegate) (mobileParty => (mobileParty.PartyComponent as GarrisonPartyComponent).InitializeGarrisonPartyProperties(mobileParty, isInitialGarrison)));
    }

    public override Hero PartyOwner => this.Settlement.OwnerClan.Leader;

    public override TextObject Name
    {
      get
      {
        if (this._cachedName == null)
        {
          this._cachedName = GameTexts.FindText("str_garrison_party_name");
          this._cachedName.SetTextVariable("MAJOR_PARTY_LEADER", this.Settlement.Name);
        }
        return this._cachedName;
      }
    }

    public override Settlement HomeSettlement => this.Settlement;

    public override int WagePaymentLimit => this.Settlement.GarrisonWagePaymentLimit;

    public override void SetWagePaymentLimit(int newLimit)
    {
      this.Settlement.SetGarrisonWagePaymentLimit(newLimit);
    }

    [SaveableProperty(1)]
    public Settlement Settlement { get; private set; }

    protected internal GarrisonPartyComponent(Settlement settlement)
    {
      this.Settlement = settlement;
    }

    protected override void OnInitialize() => this.Settlement.Town.GarrisonPartyComponent = this;

    protected override void OnFinalize()
    {
      this.Settlement.Town.GarrisonPartyComponent = (GarrisonPartyComponent) null;
    }

    public override void ClearCachedName() => this._cachedName = (TextObject) null;

    private void InitializeGarrisonPartyProperties(MobileParty mobileParty, bool isInitialGarrison)
    {
      PartyTemplateObject defaultPartyTemplate = this.Settlement.Culture.DefaultPartyTemplate;
      mobileParty.CurrentSettlement = this.Settlement;
      int troopNumberLimit = isInitialGarrison ? (int) ((double) this.Settlement.Town.Prosperity * 0.039999999105930328 + (this.Settlement.IsTown ? 40.0 : 0.0) + 80.0) : 0;
      mobileParty.InitializeMobilePartyAtPosition(defaultPartyTemplate, this.Settlement.GatePosition, troopNumberLimit);
      mobileParty.Party.SetVisualAsDirty();
      mobileParty.InitializePartyTrade(Campaign.Current.Models.ClanFinanceModel.PartyGoldLowerThreshold);
      mobileParty.Ai.DisableAi();
      mobileParty.Aggressiveness = 0.0f;
    }
  }
}
